import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams } from 'ionic-angular';
import {Storage} from "@ionic/storage";
import {AppService} from "../../../service/http.service";
import {ServeService} from "../serve.service";
import {CommonService} from "../../../provide/common.service";
import { LoadingController} from 'ionic-angular';
/**
 * Generated class for the CookBookPage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

@IonicPage()
@Component({
  selector: 'page-cook-book',
  templateUrl: 'cook-book.html',
})
export class CookBookPage {

  date;
  changeType = 1;
  userId;  //用户id
  disInfoListBefor = [];//本周菜单
  DisInfoPjListBefor = [];//本周评价
  disInfoList = [];//本周菜单
  DisInfoPjList = [];//本周评价
  loading;
  dataList=[];
  obj = {}
  constructor(public navCtrl: NavController, public navParams: NavParams, public storage: Storage, public appService: AppService,private serveSer: ServeService,
    public commonSer: CommonService , public loadingCtrl: LoadingController) {
  }

  ionViewDidLoad() {
    this.loading = this.loadingCtrl.create({
      content: '请稍等...',
      
      enableBackdropDismiss: true
   });
    this.loading.present();
    this.storage.get('userLoginInfo').then((value) => {
      this.userId = value.userid;
   });
  }
  ionViewDidEnter() {
    this.serveSer.getDisInfo({}).subscribe((res) => {
       this.disInfoList = Object.values(res)
      
    })
    this.serveSer.getDisInfoPj({}).subscribe((res) => {
      this.DisInfoPjList = res
      setTimeout(()=>{
        this.getApply()
      },300)
    })
   
  }
  //获取上周数据
  getInfoPjBefor(){
    this.serveSer.getDisInfoBefor({}).subscribe((res) => {
      this.disInfoListBefor = Object.values(res)

   })

    this.serveSer.getDisInfoPjBefor({}).subscribe((res) => {
    this.DisInfoPjListBefor = res
    this.getApplyBefor()
   })
  }
  //改变
  change(type) {
        this.loading = this.loadingCtrl.create({
            content: '请稍等...',
            
            enableBackdropDismiss: true
        });
        this.loading.present();
        this.changeType = type;
        if (this.changeType == 1) this.getApply();
        if (this.changeType == 2) this.getInfoPjBefor();
  }
  //匹配本周数据
  getApply(){
    //关联菜单 与 点赞
    this.disInfoList.map(items=>{
      items.map(item=>{
        this.DisInfoPjList.map(val=>{
          if(val.disId === item.id){
            item["delId"] = val.id
            item["pj"] = val.pj
            item["type"] = val.type
            item["userId"] = val.userId
          }
        })
      })
    })
    // this.dataList = this.disInfoList
    this.dataList = (this.mergeRow(this.disInfoList)).reverse()
    console.log(this.dataList)
    if (this.loading) {
      this.loading.dismiss();
    }
  }
  //匹配下周数据
  getApplyBefor(){
    //关联菜单 与 点赞
    this.disInfoListBefor.map(items=>{
      items.map(item=>{
        this.DisInfoPjListBefor.map(val=>{
          if(val.disId === item.id){
            item["delId"] = val.id
            item["pj"] = val.pj
            item["type"] = val.type
            item["userId"] = val.userId
          }
        })
      })
    })
    this.dataList = (this.mergeRow(this.disInfoListBefor)).reverse()
    if (this.loading) {
      this.loading.dismiss();
    }
  }

  //预处理数据
  mergeRow(lists) {
      var zoneData = []
      for(var i = 0; i<lists.length;i++){
        var list = [];
        lists[i].forEach(function (item) {
          for(var i=0;i<list.length;i++){
              if(list[i].disType === item.disType && list[i].disDate === item.disDate  ){
                  list[i].fileList.push(item);
                  return;
              }
          }
          list.push({
              disMon:item.disMon,
              disDate:item.disDate,
              disType:item.disType,
              fileList:[item]
          });
          
       });
       zoneData.push(list)
      }
      return zoneData
  }

  changeIcon(item,type){
    // 如果 pj undefine 两个可以任意点击
    // 如果 喜欢 亮  只能 点击喜欢  ，不能点击 不喜欢 ，不喜欢同理
    if(item.pj == '1'){
      if(type == '0'){
        this.commonSer.toastTime('抱歉已经点赞', '1000');
      }else{
        //调用接口取消已有喜欢的点赞
        this.obj = {id:item.delId,type:'2',userId:item.userId,disId:item.id,pj:'1'}
        this.evaluate()
      }
    }else if(item.pj == '0'){
      if(type == '1'){
        this.commonSer.toastTime('抱歉已经点赞', '1000');
      }else{
        //调用接口取消已有不喜欢的点赞
        this.obj = {id:item.delId,type:'2',userId:item.userId,disId:item.id,pj:'0'}
        this.evaluate()
      }
    }else{
      if(type == '1'){
        this.obj = {type:'1',userId:this.userId,disId:item.id,pj:'1'}
        this.evaluate()
      }else{
        this.obj = {type:'1',userId:this.userId,disId:item.id,pj:'0'}
        this.evaluate()
      }
    }
   
  }

  // 重复点赞
  getEvaluate(){
    this.serveSer.getDisInfo({}).subscribe((res) => {
      this.disInfoList = Object.values(res)
     
   })
    this.serveSer.getDisInfoPj({}).subscribe((res) => {
      this.DisInfoPjList = res
      this.getApply()
      this.commonSer.toastTime('操作成功', '1000');
    })
  }

  getEvaluateBefor(){
    this.serveSer.getDisInfoBefor({}).subscribe((res) => {
      this.disInfoListBefor = Object.values(res)

   })
    this.serveSer.getDisInfoPjBefor({}).subscribe((res) => {
      this.DisInfoPjListBefor = res
      this.getApplyBefor()
      this.commonSer.toastTime('操作成功', '1000');
    })
  }
  evaluate(){ 
     this.serveSer.getDisInfoCreate(this.obj).subscribe((res) => {
      //评价成功之后调用对应接口
      if(this.changeType == 1)  this.getEvaluate()
      else this.getEvaluateBefor()
     })
  }
}
